package com.zhanghp.class005;

import com.zhanghp.class004.SelectBubbleInsert;

import java.util.Arrays;

/**
 * @author zhanghp
 * @since 2024/2/21 9:42
 */
public class Validator {

    public static void main(String[] args) {
        validate(1000, 1000, 100);
    }

    // 生成随机数组
    public static int[] randArray(int maxSize, int maxVal) {
        int size = ((int) (Math.random() * maxSize)) + 1;
        int[] randArr = new int[size];
        for (int i = 0; i < size; i++) {
            randArr[i] = (int) (Math.random() * maxVal) + 1;
        }
        return randArr;
    }

    // 复制数组
    public static int[] copyArray(int[] source) {
        int[] copyArr = new int[source.length];
        for (int i = 0; i < copyArr.length; i++) {
            copyArr[i] = source[i];
        }
        return copyArr;
    }

    // 打印
    public static void printArr(int[] arr) {
        System.out.println(Arrays.toString(arr));
    }

    // 2个数组是否相等
    public static boolean sameArr(int[] a, int[] b) {
        if (a == null && b == null) {
            return true;
        }
        if (a == null) {
            return false;
        }
        if (b == null) {
            return false;
        }
        if (a.length != b.length) {
            return false;
        }
        for (int i = 0; i < a.length; i++) {
            if (a[i] != b[i]) {
                return false;
            }
        }
        return true;
    }

    // 数组排序校验
    public static void validate(int testTime, int maxSize, int maxVal){
        System.out.println("测试开始");
        for (int i = 0; i < testTime; i++) {
            int[] arr = randArray(maxSize, maxVal);
            int[] b = copyArray(arr);
            SelectBubbleInsert.insert(arr);
            Arrays.sort(b);
            if (!sameArr(arr, b)) {
                System.out.println("Oops");
            }
            System.out.println(i + "-结束");
        }
        System.out.println("测试结束");
    }
}
